package com.xilihui.xlh.business.activitys;

import android.content.res.Resources;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.ShareEntity;
import com.xilihui.xlh.business.manager.DownloadManager;
import com.xilihui.xlh.business.requests.SignedRequest;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.DensityUtil;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ShareUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/17.
 */

public class PromoCodeActivity extends BaseCompatActivity{
    
    @BindView(R.id.tv_hint)
    TextView tv_hint;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    
    String url = "";
    
    @Override
    public int getLayout() {
        return R.layout.activity_promo_code;
    }
    
    @Override
    public String initTitle() {
        return "我的推广码";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        String qrcodeUrl = UrlConst.baseUrl()+"/api/extend/extend_code?access_token="+
                (String) SPUtil.get(this,SPUtil.TOKEN,"");
        ImageHelper.display(this,iv_qrcode,qrcodeUrl);
    }

    @Override
    public void initData() {
        SignedRequest.weixinQRCode(this)
                .compose(DoTransform.<ShareEntity>applyScheduler(this,true))
                .compose(PromoCodeActivity.this.<ShareEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ShareEntity>(PromoCodeActivity.this) {
                    @Override
                    protected void onSuccess(ShareEntity entity) {
                        url = UrlConst.baseUrl() + entity.getUrl();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    @OnClick(R.id.tv_share)
    public void share(){
        UMImage umImage = new UMImage(this,url);
        ShareUtil.shareImage(PromoCodeActivity.this,umImage, ShareUtil.Platform.ALL);
    }
    
    @OnClick(R.id.tv_save)
    public void save(){

        DownloadManager.downloadImage(PromoCodeActivity.this,url);
        
    }
    
}
